package outfox.smt.word.alignment;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import outfox.smt.front.utils.SMTConstants.Language;
import outfox.smt.word.LangUtils;

public class Merger {
	public static long totalcount=0;
	public static void merge(String result,File file) throws IOException{
		if(file.isDirectory()){
			for(File f:file.listFiles()){
				merge(result,f);
			}
		}
		else{
			if(file.getName().equals(result)){
				System.out.println("--------");
				System.out.println("Merging "+file.getPath());
				int count=0;
				OutputStream out1=new FileOutputStream("ce.cn", true);
				OutputStreamWriter pw1=new OutputStreamWriter(out1,"utf-8");
				OutputStream out2=new FileOutputStream("ce.en", true);
				OutputStreamWriter pw2=new OutputStreamWriter(out2,"utf-8");
				BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"utf-8"));
				String csen;
				while((csen=br.readLine())!=null){
					String osen=br.readLine();
					if(LangUtils.guessLanguage(csen)==Language.ZH_CN){
						count++;
						pw1.write(csen);
						pw1.write("\n");
						pw2.write(osen);
						pw2.write("\n");
					}
				}
				totalcount+=count;
				System.out.println("Merged "+count+" sentences");
				pw1.flush();
				pw1.close();
				pw2.flush();
				pw2.close();
				br.close();
			}
		}
	}
	public static void main(String args[]) throws IOException{
		merge("ce.txt",new File("F:/"));
		System.out.println("********");
		System.out.println("Total count: "+totalcount);
	}
}
